package com.thread.thread.AbstractQueuedSynchronizer;


import sun.applet.Main;

import java.util.concurrent.locks.Lock;
import java.util.stream.IntStream;

public class Test {
	public static  int a=0;
	public static void main(String[] args) throws  Exception{
		Thread[] thread=new Thread[1000];
		Lock lock=new CaiCaiLock();
		/*IntStream.range(0,thread.length).forEach(i->{
				thread[i]=new Thread(()->{
					synchronized (Main.class){
						System.out.println("当前线程"+i);
						for(int j=0;j<100;j++){
							a=a+1;
						}
					}
				});

		});*/

		IntStream.range(0,thread.length).forEach(i->{
			thread[i]=new Thread(()->{

				try {
					lock.lock();
					System.out.println("当前线程"+i);
					for(int j=0;j<100;j++){
						a=a+1;
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}


			});

		});
		IntStream.range(0,thread.length).forEach(i->{
			thread[i].start();

		});

		IntStream.range(0,thread.length).forEach(i->{
			try {
				thread[i].join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		});
		System.out.println(a);

	}




}
